package com.oj.judge.strategy.impl;

import com.oj.judge.domain.JudgeContext;
import com.oj.judge.domain.JudgeResult;
import com.oj.judge.enums.JudgeResultMessageEnum;
import com.oj.judge.strategy.JudgeStrategy;
import com.oj.problem.domain.Problem;
import com.oj.problem.domain.TestCase;

import java.util.List;

/**
 * @Classname DefaultJudgeStrategyImpl
 * @Description TODO
 * @Date 2025/3/18 21:15
 * @Created by xxx
 */
public class DefaultJudgeStrategyImpl implements JudgeStrategy {
    @Override
    public JudgeResult doJudge(JudgeContext judgeContext) {
        List<String> inputList = judgeContext.getInputList();
        List<String> outputList = judgeContext.getOutputList();
        List<TestCase> testCases = judgeContext.getTestCases();
        Problem problem = judgeContext.getProblem();
        Integer time = judgeContext.getJudgeResult().getTime();
        Integer memory = judgeContext.getJudgeResult().getMemory();
        // 校验输出
        if (outputList.size() != testCases.size()) {
            return new JudgeResult(JudgeResultMessageEnum.RuntimeError.getValue(), time, memory, "输出数量与测试用例数量不匹配");
        }
        // 校验每一项输出和预期输出是否相等
        for (int i = 0; i < outputList.size(); i++) {
            String expectedOutput = testCases.get(i).getOutput();
            String actualOutput = outputList.get(i);
            if (!expectedOutput.equals(actualOutput)) {
                return new JudgeResult(JudgeResultMessageEnum.WrongAnswer.getValue(), time, memory, "输出与测试用例不匹配testCaseId:" + i + " 期望输出:" + expectedOutput + " 实际输出:" + actualOutput);
            }
        }
        // 判断题目的限制时间和空间
        Integer timeLimit = problem.getTimeLimit();
        Integer memoryLimit = problem.getMemoryLimit();
        if (time > timeLimit) {
            return new JudgeResult(JudgeResultMessageEnum.TimeLimitExceeded.getValue(), time, memory, "时间超出限制");
        }
        if (memory > memoryLimit) {
            return new JudgeResult(JudgeResultMessageEnum.MemoryLimitExceeded.getValue(), time, memory, "空间超出限制");
        }
        // 判题通过
        return new JudgeResult(JudgeResultMessageEnum.Accepted.getValue(), time, memory, "通过");
    }
}
